package pub.elephants.socket.netty.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import pub.elephants.net.body.ElephantsRequest;
import pub.elephants.net.body.ElephantsResponse;
import pub.elephants.socket.NetServerFactory;

public class NettyServerHandler extends SimpleChannelInboundHandler<ElephantsRequest> {

    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ElephantsRequest request) throws Exception {
        // invoke
        ElephantsResponse response = NetServerFactory.invokeService(request, null);
        channelHandlerContext.writeAndFlush(response);
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        ctx.close();
    }
}
